#*********************************************************************************
#      Copyright:  (C) 2022 GuoWenxue <guowenxue@gmail.com>
#                  All rights reserved.
#
#       Filename:  Makefile
#    Description:  This Makefile used to compile all the C source code file in
#                  current folder to a excutable binary file.
#
#********************************************************************************/

PRJ_PATH=$(shell pwd)
APP_NAME = websocket

#CROSSTOOL=arm-linux-gnueabihf-

# C source files in top-level directory
SRCFILES = $(wildcard *.c)

# common CFLAGS for our source code
CFLAGS = -Wall -Wshadow -Wundef -Wmaybe-uninitialized -D_GNU_SOURCE

# C source file in sub-directory
DIRS= wss
DIRS_PATH=$(patsubst %,${PRJ_PATH}/%,$(DIRS))
CFLAGS+=$(patsubst %,-I%,$(DIRS_PATH))
LDFLAGS+=$(patsubst %,-L%,$(DIRS_PATH))
LIBS=$(patsubst %,-l%,$(DIRS))

# CFLAGS and LDFLAGS for opensource library in libs
CFLAGS+=-I ${PRJ_PATH}
CFLAGS+=-I ${PRJ_PATH}/libs/install/include/
LDFLAGS+=-L ${PRJ_PATH}/libs/install/lib
LIBS+=-levent -levent_core -static -lpthread -lm

.PHONY:libs
all: libs modules binary

libs:
	cd libs && ./build.sh ${CROSSTOOL}

modules:
	@set -e; for d in ${DIRS}; do $(MAKE) CROSSTOOL=${CROSSTOOL} CFLAGS="${CFLAGS}" -C $${d}; done

binary:  ${SRCFILES}
	$(CROSSTOOL)gcc $(CFLAGS) -o ${APP_NAME} $^ ${LDFLAGS} ${LIBS}
	@echo " Compile over"

clean:
	set -e; for d in ${DIRS}; do $(MAKE) clean -C $${d}; done
	@rm -f *.o $(APP_NAME)

distclean: clean
	@rm -rf cscope* tags
	@cd libs && ./build.sh distclean
